package com.hspedu.polymorphic_.dynamic_;

public class DynamicBinding {
    public static void main(String[] args) {
        A a = new B();//向上转型
        System.out.println(a.sum());//a的运行类型是B(), i=20, 调用B里面的sum()，打印出40
        System.out.println(a.sum1());//30
    }
}

class A{
    public int i = 10;
    public int sum(){
        return getI()+10;
    }
    public int sum1(){
        return i + 10;
    }
    public int getI(){
        return i;
    }
}

class B extends A{
    public int i = 20;
    public int sum(){
        return i +20;
    }
    public int getI(){
        return i;
    }
    public int sum1(){
        return i + 10;
    }
}